package com.cf.api.db;


import com.alibaba.druid.pool.DruidDataSource;
import com.md.common.utils.dao.config.DBProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.beans.PropertyVetoException;
import java.sql.SQLException;

/**
 * 功能描述：数据库初始化
 *
 * @author lujianping@kuparts.com
 * @version 1.0
 * @date 2017/9/14 17:30
 * @since JDK 1.8
 */
@Configuration
public class DBConfig {
    @Autowired
    private DBProperties dBProperties;
    @Autowired
    private OtherDBProperties otherDBProperties;

    @Bean(name = "dataSource")
    public DruidDataSource dataSource() throws PropertyVetoException, SQLException {
        DruidDataSource druidDataSource = new DruidDataSource();
        druidDataSource.setUrl(dBProperties.getUrl());
        druidDataSource.setUsername(dBProperties.getUsername());
        druidDataSource.setPassword(dBProperties.getPassword());
        druidDataSource.setDriverClassName(dBProperties.getDriverClassName());
        druidDataSource.setFilters(dBProperties.getFilters());
        druidDataSource.setMaxActive(dBProperties.getMaxActive());
        druidDataSource.setInitialSize(dBProperties.getInitialSize());
        druidDataSource.setMaxWait(dBProperties.getMaxWait());
        druidDataSource.setTimeBetweenEvictionRunsMillis(dBProperties.getTimeBetweenEvictionRunsMillis());
        druidDataSource.setMinEvictableIdleTimeMillis(dBProperties.getMinEvictableIdleTimeMillis());
        druidDataSource.setValidationQuery(dBProperties.getValidationQuery());
        druidDataSource.setTestWhileIdle(dBProperties.isTestWhileIdle());
        druidDataSource.setTestOnBorrow(dBProperties.isTestOnBorrow());
        druidDataSource.setTestOnReturn(dBProperties.isTestOnReturn());
        druidDataSource.setMaxOpenPreparedStatements(dBProperties.getMaxOpenPreparedStatements());
        druidDataSource.setRemoveAbandoned(dBProperties.isRemoveAbandoned());
        druidDataSource.setRemoveAbandonedTimeout(dBProperties.getRemoveAbandonedTimeout());
        druidDataSource.setLogAbandoned(dBProperties.isLogAbandoned());
        return druidDataSource;
    }

    @Bean(name = "otherDataSource")
    public DruidDataSource otherDataSource() throws PropertyVetoException, SQLException {
        DruidDataSource druidDataSource = new DruidDataSource();
        druidDataSource.setUrl(otherDBProperties.getUrl());
        druidDataSource.setUsername(otherDBProperties.getUsername());
        druidDataSource.setPassword(otherDBProperties.getPassword());
        druidDataSource.setDriverClassName(otherDBProperties.getDriverClassName());
        druidDataSource.setFilters(otherDBProperties.getFilters());
        druidDataSource.setMaxActive(otherDBProperties.getMaxActive());
        druidDataSource.setInitialSize(otherDBProperties.getInitialSize());
        druidDataSource.setMaxWait(otherDBProperties.getMaxWait());
        druidDataSource.setTimeBetweenEvictionRunsMillis(otherDBProperties.getTimeBetweenEvictionRunsMillis());
        druidDataSource.setMinEvictableIdleTimeMillis(otherDBProperties.getMinEvictableIdleTimeMillis());
        druidDataSource.setValidationQuery(otherDBProperties.getValidationQuery());
        druidDataSource.setTestWhileIdle(otherDBProperties.isTestWhileIdle());
        druidDataSource.setTestOnBorrow(otherDBProperties.isTestOnBorrow());
        druidDataSource.setTestOnReturn(otherDBProperties.isTestOnReturn());
        druidDataSource.setMaxOpenPreparedStatements(otherDBProperties.getMaxOpenPreparedStatements());
        druidDataSource.setRemoveAbandoned(otherDBProperties.isRemoveAbandoned());
        druidDataSource.setRemoveAbandonedTimeout(otherDBProperties.getRemoveAbandonedTimeout());
        druidDataSource.setLogAbandoned(otherDBProperties.isLogAbandoned());
        return druidDataSource;
    }

}
